/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package Storage;

import java.util.HashMap;
import java.util.Vector;

/**
 * A term class holds a set of meta information and a prefix. Changes must be made via its vocabulary.
 * 
 * @author Christoph Schnack
 *
 */
public class TermClass{
	private HashMap<String,Vector<String>> elements = new HashMap<String, Vector<String>>();
	private String preFixName;
	private Vector<String> preFixVal;
	private Vector<String> sortedKeys = new Vector<String>();
	
	/**
	 * Generates an array of the meta information keys if available.
	 * @return keys or null if no meta information
	 */
	public String[] getKeys(){ return (!elements.isEmpty())? elements.keySet().toArray(new String[]{}):null;}
	
	public void addKey(String s){
		if(!sortedKeys.contains(s))sortedKeys.add(s);
	}
	
	public void replaceKey(String old, String n){
		if(sortedKeys!=null && sortedKeys.size()>0){
			for(int i=0;i<sortedKeys.size();i++){
				if(sortedKeys.elementAt(i).equals(old)){
					sortedKeys.set(i,n);
					break;
				}
			}
		}
	}
	
	public void removeKey(String k){
		sortedKeys.remove(k);
	}
	
	public void deleteKey(String k){
		removeKey(k);
		elements.remove(k);
	}
	
	public boolean hasKey(String k){
		return (elements==null)?false:elements.containsKey(k);
	}
	
	public void setSortedKeys(Vector<String> arg){
		sortedKeys = arg;
	}
	
	public Vector<String> getSortedKeys(){
		return sortedKeys;
	}
	
	public boolean isEmpty(){
		return (elements==null)? true:elements.isEmpty();
	}
	
	public void putElement(String name,Vector<String> arg){
		elements.put(name,arg);
		addKey(name);
	}
	
	public Vector<String> getValues(String name){
		return elements.get(name);
	}
	
	public void removeElement(String name){
		elements.remove(name);
		removeKey(name);
	}
	
	public void putElements(HashMap<String,Vector<String>> arg){
		elements = arg;
	}
	
	public void setPreFix(String name, Vector<String> vals ){
		preFixName = name;
		preFixVal = vals;
	}
	public void addPreFixValue(String valueArg){
		if(preFixVal!=null && !preFixVal.contains(valueArg)) preFixVal.add(valueArg);
	}
	
	public String getPreFixName(){return preFixName;}
	public Vector<String> getPreFixVal(){return preFixVal;}
	
	public boolean hasPrefix(){
		if(preFixName==null)return false;
		else return true;
	}

}
